#!/bin/bash

# 检查参数数量
if [ "$#" -ne 3 ]; then
    echo "Usage: $0 <old_message> <new_message> <git_filter_repo_path>"
    exit 1
fi

# 参数
OLD_MESSAGE=$1
NEW_MESSAGE=$2
GIT_FILTER_REPO_PATH="./git_filter_repo.py"  # git-filter-repo 的路径

# 验证 git-filter-repo 是否存在
if [ ! -f "$GIT_FILTER_REPO_PATH" ]; then
    echo "Error: git-filter-repo not found at $GIT_FILTER_REPO_PATH"
    exit 1
fi

# 构建并执行 git filter-repo 命令
MESSAGE_CALLBACK=$(cat <<EOF
return message.replace(b"$OLD_MESSAGE", b"$NEW_MESSAGE")
EOF
)

python "$GIT_FILTER_REPO_PATH" --message-callback "$MESSAGE_CALLBACK"

# 检查结果
if [ $? -eq 0 ]; then
    echo "Commit messages modified successfully."
else
    echo "Error occurred while modifying commit messages."
fi

# 匹配所有的字段 可以通过 --refs来控制指定范围的分支或代码
# bash modifyMessage.sh du ddd ./git_filter_repo.py